﻿using Magicodes.ExporterAndImporter.Excel.AspNetCore;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NP.BPMReportPlatform.Reports.ZPSPXT;
using NP.BPMReportPlatform.Reports.ZPSPXT.Dtos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using NP.BPMReportPlatform.Entities.BPM_Business;
using Magicodes.ExporterAndImporter.Core;
using Microsoft.AspNetCore.Http;
using NP.BPMReportPlatform.Reports.OrderMaterialDetail.Dtos;
using Magicodes.ExporterAndImporter.Excel;

namespace NP.BPMReportPlatform.Reports.ZPSPXT
{
    /// <summary>
    /// 招聘审批系统配置表
    /// </summary>
    [Authorize]
    [ApiController]
    [Route("api/Reports/ZPSPXTConfig")]
    public class ZPSPXTConfigController : BPMReportPlatformController
    {
        private readonly IZPSPXTConfigService _service;
        private readonly IExcelImporter _importer;
        public ZPSPXTConfigController(IZPSPXTConfigService service, IExcelImporter importer)
        {
            _service = service;
            _importer = importer;
        }

        /// <summary>
        /// 报表查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("GetAll")]
        public async Task<PagedResultDto<ZPSPXTConfigDto>> GetAll([FromBody] ZPSPXTConfigInput input)
        {
            return await _service.GetAllAsync(input);
        }

        /// <summary>
        /// 报表导出
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("ExportExcel")]
        public async Task<XlsxFileResult<ZPSPXTConfigDto>> ExportExcel([FromBody] ZPSPXTConfigInput input)
        {
            var rst = await _service.ExportAsync(input);
            return new XlsxFileResult<ZPSPXTConfigDto>(rst, "导出Excel");
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("Delete")]
        public async Task Delete(Guid id)
        {
            await _service.Delete(id);
        }

        /// <summary>
        /// 编辑/新增
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("Edit")]
        public async Task Edit(ZPSPXTConfigEditInput input)
        {
            await _service.Edit(input);
        }

        /// <summary>
        /// 导出导入模板
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("ExportTemplate")]
        public async Task<FileResult> ExportTemplate()
        {
            var rst = await _service.ExportTemplate();
            return File(rst, "application/octet-stream", "招聘审批系统配置表导入模板.xlsx");
        }

        /// <summary>
        /// 导入接口
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("ImportExcel")]
        public async Task<ImportResultExt<ZPSPXTConfigDto>> ImportExcel(IFormFile file)
        {
            if (file == null)
            {
                var res = new ImportResultExt<ZPSPXTConfigDto>
                {
                    Exception = new Exception("请上传文件！")
                };
                return res;
            }
            var stream = file.OpenReadStream();
            var rst = await _importer.Import<ZPSPXTConfigDto>(stream);
            stream.Close();
            if (rst.HasError)
            {
                var resp = new ImportResultExt<ZPSPXTConfigDto>()
                {
                    Exception = rst.Exception,
                    RowErrors = rst.RowErrors,
                    TemplateErrors = rst.TemplateErrors
                };
                return resp;
            }
            else
            {
                return await _service.ImportExcel(rst.Data.ToList());
            }
        }
    }
}
